<?php
namespace app\admin\logic;

class LoginLogic extends BaseLogic{

    /**
     * 检查超级管理员登录规则
     * @param $params
     * @return mixed
     */
    public function check_admin_login_rule($params){
        if(empty($params['username'])){
            return $this->get_response(0,'用户名必须填写');
        }
        if(empty($params['password'])){
            return $this->get_response(0,'密码必须填写');
        }

        $maps = [];
        $maps[] = ['role','=',1];
        $maps[] = ['username','=',$params['username']];
        $detail = model('user')->get_detail($maps,'id,openid,username,password,role');
        if(empty($detail)){
            return $this->get_response(0,'该用户不存在');
        }

        $password = md5($params['password']);
        if($detail['password'] != $password){
            return $this->get_response(0,'密码错误');
        }

        $data['detail'] = $detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查更新密码规则
     * @param $admin_openid
     * @param $params
     * @return mixed
     */
    public function check_save_password_rule($admin_openid,$params){
        if(empty($params['new_password'])){
            return $this->get_response(0,'新密码必须填写');
        }
        if(empty($params['sure_password'])){
            return $this->get_response(0,'确认密码必须填写');
        }
        if($params['new_password'] != $params['sure_password']){
            return $this->get_response(0,'新密码与确认密码不一致');
        }

        $maps = [];
        $maps[] = ['openid','=',$admin_openid];
        $maps[] = ['role','=',1];
        $detail = model('user')->get_detail($maps,'id,password');
        if(empty($detail)){
            return $this->get_response(0,'该用户不存在');
        }

        $data['detail'] = $detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查二维码登录规则
     * @param $params
     * @return mixed
     */
    public function check_qrcode_login_rule($params){
        if(empty($params['code'])){
            return $this->get_response(0,'code必须填写');
        }

        //检查二维码
        $maps = [];
        $maps[] = ['code','=',$params['code']];
        $alc_detail = model('admin_login_code')->get_detail($maps);
        if(empty($alc_detail)){
            return $this->get_response(0,'登录失败');
        }
        $now_times = time();
        $c_times = strtotime($alc_detail['c_time']);
        if($now_times - $c_times >= 300){
            return $this->get_response(-1,'该二维码已过期');
        }
        if(empty($alc_detail['openid'])){
            return $this->get_response(-2,'该二维码未使用');
        }

        //检查用户
        $maps = [];
        $maps[] = ['openid','=',$alc_detail['openid']];
        $maps[] = ['role','in',[2,3]];
        $u_detail = model('user')->get_detail($maps,'id,openid,username,role,enable');
        if(empty($u_detail)){
            return $this->get_response(0,'该用户不是管理员');
        }
        if($u_detail['enable'] == 0){
            return $this->get_response(0,'该用户已禁用');
        }

        $data['u_detail'] = $u_detail;
        return $this->get_response(1,'OK',$data);
    }
}